/*
 * Copyright 2012 Banaanvraag.
 *
 * MIT License, details are found in the LICENSE text file
 */

package banaanvraag.tools.logging;

import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

/**
 * Use this Handler when in test environments. Using it in production environments
 * results in quite the overhead in terms of console output!
 * @author Wukl
 */
public class DebugHandler extends Handler {

    long startTime, publishTime;
    
    public DebugHandler() {
        startTime = System.currentTimeMillis();
    }
    
    @Override
    public void publish(LogRecord record) {
        publishTime = record.getMillis() - startTime;
        if(record.getLevel() != Level.SEVERE) {
            System.out.println("[" + publishTime + "] " + record.getLevel()
                    .getName() + ": " + record.getMessage());
        }
        else {
            System.out.println("\u001B[31m[" + publishTime + "] " + record
                    .getLevel().getName() + ": " + record.getMessage());
            System.out.println("\u001B[31mAt " + record.getSourceClassName() 
                    + "." + record.getSourceMethodName());
            System.exit(1);
        }
    }

    @Override
    public void flush() {}

    @Override
    public void close() throws SecurityException {}
    
}